export default function (sqlite) {
  return {
    // 插入经销商等级
    insertDealerLevel(dealerLevelName, isDividend) {
      return new Promise((resolve, reject) => {
        // 检查 dealer_level_name 是否已存在
        const checkSql = 'SELECT COUNT(*) as count FROM dealer_level WHERE dealer_level_name = ?'
        sqlite.get(checkSql, [dealerLevelName], (checkErr, row) => {
          if (checkErr) {
            console.error('Error checking dealer_level_name:', checkErr.message)
            reject('Error checking dealer_level_name')
          } else if (row.count > 0) {
            // 如果 dealer_level_name 已存在
            resolve('等级名称重复')
          } else {
            // 插入新的 dealer_level 记录
            const insertSql =
              'INSERT INTO dealer_level (dealer_level_name, is_dividend) VALUES (?, ?)'
            sqlite.run(insertSql, [dealerLevelName, isDividend], function (err) {
              if (err) {
                console.error('Error inserting dealer_level:', err.message)
                reject(err.message)
              } else {
                resolve(this.lastID) // 返回新插入记录的ID
              }
            })
          }
        })
      })
    },

    // 编辑经销商等级
    editDealerLevel(dealerLevelId, dealerLevelName, isDividend) {
      return new Promise((resolve, reject) => {
        // 检查新的 dealer_level_name 是否与其他记录重复
        const checkSql =
          'SELECT COUNT(*) as count FROM dealer_level WHERE dealer_level_name = ? AND dealer_level_id != ?'
        sqlite.get(checkSql, [dealerLevelName, dealerLevelId], (checkErr, row) => {
          if (checkErr) {
            console.error('Error checking dealer_level_name:', checkErr.message)
            reject('Error checking dealer_level_name')
          } else if (row.count > 0) {
            // 如果 dealer_level_name 已存在
            resolve('等级名称重复')
          } else {
            // 更新 dealer_level 记录
            const updateSql =
              'UPDATE dealer_level SET dealer_level_name = ?, is_dividend = ? WHERE dealer_level_id = ?'
            sqlite.run(updateSql, [dealerLevelName, isDividend, dealerLevelId], function (err) {
              if (err) {
                console.error('Error updating dealer_level:', err.message)
                reject(err.message)
              } else {
                resolve(this.changes > 0) // 返回更新成功状态
              }
            })
          }
        })
      })
    },

    // 获取所有经销商等级
    getAllDealerLevel() {
      return new Promise((resolve, reject) => {
        const sql = 'SELECT * FROM dealer_level'
        sqlite.all(sql, [], (err, rows) => {
          if (err) {
            console.error('Error fetching dealer_level:', err.message)
            reject(err.message)
          } else {
            resolve(rows)
          }
        })
      })
    },

    // 删除经销商等级
    deleteDealerLevel(dealerLevelId) {
      return new Promise((resolve, reject) => {
        const sql = 'DELETE FROM dealer_level WHERE dealer_level_id = ?'
        sqlite.run(sql, [dealerLevelId], function (err) {
          if (err) {
            console.error('Error deleting dealer_level:', err.message)
            reject(err.message)
          } else {
            resolve(this.changes > 0)
          }
        })
      })
    }
  }
}
